package com.minio.util;

import com.minio.config.AliOSSProperties;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class AliOSSUtils {



    public String upload(MultipartFile file,AliOSSProperties aliOSSProperties){

        //获取阿里云OSS参数
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();

        // 获取上传的文件的输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (inputStream == null){
            return "/error.png";
        }

//        // 避免文件覆盖，需要使用UUID将文件重命名
//        String originalFilename = file.getOriginalFilename();
//        String fileName = datePath() +"/" + UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//
//        //上传文件到 OSS
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
//        ObjectMetadata meta=new ObjectMetadata();
//        meta.setContentType(file.getContentType());
//
//
//        ossClient.putObject(bucketName,fileName,inputStream,meta);
//        //文件访问路径
//        String url =  aliOSSProperties.getHost() +"/" + fileName;
//        // 关闭ossClient
//        ossClient.shutdown();
        // 把上传到oss的路径返回
        return "";
    }

    public String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

}
